package main

import (
	"errors"
	"fmt"
)

func main() {
	result := add(1, 2)
	fmt.Println(result)
	if result2, err := add2(1, -1); err != nil {
		fmt.Println("err:", err)
	} else {
		fmt.Println(result2)
	}
	result3, _ := add3(1, 2)
	fmt.Println(result3)
	fmt.Println(add4(1, 2, 3, 4, 5))
	sum2 := func(a, b int) int {
		return a + b
	}
	fmt.Println(sum2(10, 20))

	c1 := colsure()
	fmt.Println(c1())
	fmt.Println(c1())
	fmt.Println(c1())

	var age Age = 25
	age.String()
	(&age).Modify()
	age.String()
	sm := Age.String
	sm(age)
}
func add(a int, b int) int {
	return a + b
}

func add2(a, b int) (int, error) {
	if a < 0 || b < 0 {
		return 0, errors.New("a或b不能为负数！")
	}
	return a + b, nil
}
func add3(a, b int) (sum int, err error) {
	if a < 0 || b < 0 {
		sum = 0
		err = errors.New("a或b不能为负数！")
		return
	} else {
		sum = a + b
		err = nil
		return
	}
}
func add4(params ...int) int {
	sum := 0
	for _, v := range params {
		sum += v
	}
	return sum
}

func colsure() func() int {
	i := 0
	return func() int {
		i++
		return i
	}
}

type Age uint

func (age Age) String() {
	fmt.Println("the age is ", age)
}
func (age *Age) Modify() {
	*age = 30
}
